package com.mindata.blockchain.socket.packet;

import com.mindata.blockchain.ApplicationContextProvider;
import com.mindata.blockchain.core.event.ClientRequestEvent;
import com.mindata.blockchain.core.manager.DbBlockManager;
import com.mindata.blockchain.socket.body.RpcSimpleBlockBody;

/**
 * 构建向别的节点请求next block的builder.带着自己最后一个block的hash
 * @author wuweifeng wrote on 2018/3/20.
 */
public class NextBlockPacketBuilder {
	public static BlockPacket build() {
		return build(null);
	}

	public static BlockPacket build(String responseId) {
		String hash = ApplicationContextProvider.getBean(DbBlockManager.class).getLastBlockHash();

		RpcSimpleBlockBody rpcBlockBody = new RpcSimpleBlockBody(hash);
		rpcBlockBody.setResponseMsgId(responseId);

		PacketBuilder<RpcSimpleBlockBody> packetBuilder = new PacketBuilder<>();
		packetBuilder.setType(PacketType.NEXT_BLOCK_INFO_REQUEST);
		packetBuilder.setBody(rpcBlockBody);
		BlockPacket blockPacket = packetBuilder.build();

		// 发布client请求事件
		ApplicationContextProvider.publishEvent(new ClientRequestEvent(blockPacket));
		return blockPacket;
	}

}
